from fastapi import APIRouter, Depends, Request
from app.core.responses import Response
from app.services.auth import get_current_user
from schemas.order import Payment, Create
from app.services.order import OrderServices

router = APIRouter(prefix="/order_used", dependencies=[Depends(get_current_user)])


@router.post("/service/{order_id}", summary="服务使用")
async def index(current: int = 1, pageSize: int = 10):
    data = OrderServices.get_all(current, pageSize).serialize()
    return Response.success(data)


@router.post("", summary="创建订单")
async def store(form: Create, request: Request):
    user = request.state.user
    return OrderServices.create(form, user['id'])


@router.post("/payment", summary="微信支付")
async def store(form: Payment, request: Request):
    user = request.state.user
    return OrderServices.payment_order(form.order_id, user['id'])


# @router.get("/{Id}",summary="详情")
# async def show(Id: int):
#     data = CategoryServices.get_one(Id).serialize()
#     poster = data['poster'][0]
#     desc_image = data['desc_image'][0]
#     data['desc_image'] = [{"url":desc_image}]
#     data['poster'] = [{"url":poster}]
#     return Response.success(data)


@router.patch("/{id}", summary="更新")
async def update():
    return Response.success({})


@router.delete("/{Id}", summary="删除")
async def delete(Id: int):
    try:
        OrderServices.delete(Id)
    except Exception as e:
        return Response.success({"message": "删除失败"})
    return Response.success({"message": "删除成功"})
